package inventoryServe.controller;


import inventoryServe.po.Product;
import inventoryServe.po.Role;
import inventoryServe.po.Supplier;
import inventoryServe.po.Type;
import inventoryServe.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import inventoryServe.utils.*;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-06-22
 */
@Controller
public class ProductController {
    @Autowired
    private ProductService productService;

    // 获取产品数据
    @GetMapping("/products")
    @ResponseBody
    public Object allProduct(HttpServletRequest req) {

        String name = req.getParameter("name").trim();
        String typeId = req.getParameter("typeId").trim();
        System.out.println(name);
        Product product = new Product();
        if (name != "") {
            product.setName(name);
        }
        if (typeId != "") {
           Type type = new Type();
           type.setId(Integer.parseInt(typeId));
            product.setType(type);
        }



        System.out.println("查询产品");
        List<Product> res = this.productService.allProduct(product);
        System.out.println(res);
        if (res != null) {
            return ResUtils.Success(1, "查询成功", res);
        } else {
            return ResUtils.Fail(0, "没有数据");
        }
    }

    // 根据 id 获取供应商信息
    @GetMapping("/products/{id}")
    @ResponseBody
    public Object productOfId(@PathVariable("id") Integer id) {

        Product res = productService.productOfId(id);
        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }
    }


    // 删除供应商
    @DeleteMapping("products/{id}")
    @ResponseBody
    public Object deleteProduct(@PathVariable("id") Integer id) {
        boolean res = productService.deleteProduct(id);

        if (res) {
            return ResUtils.Success(1,"删除成功");
        } else {
            return ResUtils.Fail(0,"删除失败");
        }
    }

    // 增加供应商
    @PostMapping("products/")
    @ResponseBody
    public Object addProduct(@RequestBody Product product) {
        System.out.println(product);

        Date now = new Date();
        product.setCreateTime(now);
        product.setUpdateTime(now);
        System.out.println(product);
        boolean res = productService.addProduct(product);

        if (res) {
            return ResUtils.Success(1,"添加成功",product);
        } else {
            return ResUtils.Fail(0,"添加失败");
        }
    }


    // 根据 id 修改供应商信息
    @PutMapping("/products/{id}")
    @ResponseBody
    public Object editProduct(@PathVariable("id") Integer id, @RequestBody Product productr) {

        Date now = new Date();
        productr.setUpdateTime(now);

        System.out.println(productr);

        Boolean res = productService.editProduct(productr);

        if (res) {
            return ResUtils.Success(1, "修改成功",productr);
        } else {
            return ResUtils.Fail(0, "修改失败");
        }
    }

}
